﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Diagnostics;
using System.ComponentModel;
using WPFExcelReport.Helpers;
using System.Windows.Input;
namespace WPFExcelReport
{
    public partial class TWTWV
    {
        public TWTWV()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWTEINZ();
            this.DataContext = MV;
            LMS = "W";
            InitializeComponent(); 
        }
        public override void LOADBYAUF()
        {
            if (!IsCanLoad)
                return;
            TBMain.Clear();
            if (ORDERNUM.Length < 15)
                return;
            dwData = TBMain.NewRow();
            dwData["WTEEINZNR"] = (app.Mandant + ORDERNUM.Trim()).PadRight(23) + "W";
            SResult = TMV.QWTWV(dwData);
            string pattern = "";
            string pos = "";
            if (SResult.Tables[0].Rows.Count == 0)
                return;
            int dfs = Convert.ToInt16(SResult.Tables[0].Rows[0]["VALUE"].ToString());
            LOADPATTERN(SResult,dfs);
            return;
            //if (dfs != Convert.ToInt16(DFS))
            {
                //InitialTable(dfs.ToString());//准备空表
            }
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                double line = Convert.ToDouble(row["TXTKPOS"].ToString());
                pattern = row["TXTKTEXT"].ToString();
                int lg = pattern.Trim().Length;
                string pv = "0";
                for (int idx = 0; idx < dfs; idx++)
                {
                    pos = Helper.Right("0000" + idx, 4);
                    pv = pattern.Substring(idx, 1);
                    if (pv == " ")
                        pv = "0";
                    row["LINE"] = (idx+1) * 10;
                    row[LMS + pos] = pv;
                }
            }
            MergeData(SResult, true);
        }

        public override void OnSave()
        {
            if (!IsSaved)
                return;
            foreach (DataRow row in TBMain.Rows)
            {
                string pattern = "";
                int dfs = Convert.ToInt16(DFS);
                for (int idx = 0; idx < dfs; idx++)
                {
                    string pos = "";
                    pos = Helper.Right("0000" + idx, 4);
                    pattern += row[LMS + pos].ToString();
                }
                row["TXTKTEXT"] = Helper.Left(pattern + "000000000000000000000000000000000000000000000000000000000000000000000000", 72);
                TMV.VTK.UpdateTXTKON(row);
            }
            base.OnSave();
        }
        public override void OnDFSChg(string _dfs)
        {
            try
            {
                base.OnDFSChg(_dfs);
                if (!IsEdit)
                    return;
                InitialTable(_dfs);
             }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            } 
        }

        DataRow dwDF = null;
        DataRow dwPG = null;

        public override void CHKWEAVE()
        {
            if (TBDF == null)
                return;
            if (TBPG == null)
                return;
            int pg = TBPG.Rows.Count;
            if (pg != Convert.ToInt16(DFS))
                return;
            pg = TBDF.Rows.Count;
            if (pg != Convert.ToInt16(DFS))
                return;
            if (TBMain.Rows.Count == 0)
                return;

            int dfs = Convert.ToInt16(DFS);
            string up = "0";
            string yarn = "0";
            int weft = 0;
            foreach (DataRow dwpg in TBPG.Rows)
            {
                dwData = TBMain.Rows[weft];
                //便利纹板，也就是开始织造，分别是插入纬纱顺序
                for (int idx = 0; idx < dfs; idx++)
                {
                    string pos = Helper.Right("0000" + idx, 4);
                    dwData[LMS + pos] = 0;
                    up = dwpg["P" + pos].ToString();//1为上
                    if (up == "0")
                        continue;
                    dwDF = TBDF.Rows[idx];
                    yarn = dwDF["D" + pos].ToString();//1为有纱
                    if (yarn == "1")
                    {
                        dwData[LMS + pos] = 1;
                    }
                }
                weft++;
            }
        }
        //穿综变化
        public override void OnDFPSChg()
        {
             
         
            CHKWEAVE();

        }
        //纹板变化
        public override void OnPGPSChg()
        {
            try
            { 
                if (TBDF == null)
                    return;
                if (TBPG == null)
                    return; 
                if (PGPS == "-1")
                    return;
                string[] pgs = PGPS.Split(',');
                int dfs = Convert.ToInt16(DFS);
                string up = "0";
                string yarn = "0";
                int weft = Convert.ToInt16(pgs[0]) / 10 - 1;
                DataRow dwpg = TBPG.Rows[weft];

                dwData = TBMain.Rows[weft];
                //便利纹板，也就是开始织造，分别是插入纬纱顺序
                for (int idx = 0; idx < dfs; idx++)
                {
                    string pos = Helper.Right("0000" + idx, 4);
                    dwData[LMS + pos] = 0;
                    up = dwpg["P" + pos].ToString();//1为上
                    if (up == "0")
                        continue;
                    dwDF = TBDF.Rows[idx];
                    yarn = dwDF["D" + pos].ToString();//1为有纱
                    if (yarn == "1")
                    {
                        dwData[LMS + pos] = 1;
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

    }
}
